
/**
 * Copyright 2016 Yonyou Corporation Ltd. All Rights Reserved.
 * This software is published under the terms of the Yonyou Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 *
 * @Project Name : dms.manage
 * @File name : RoleCtrlServiceImpl.java
 * @Author : yll
 * @Date : 2016年8月8日
 * <p>
 * ----------------------------------------------------------------------------------
 * Date       Who       Version     Comments
 * 1. 2016年8月8日    yll    1.0
 * <p>
 * <p>
 * <p>
 * <p>
 * ----------------------------------------------------------------------------------
 */

package com.yonyou.cyxdms.sysAuth.service.service.role;


import com.yonyou.cyx.framework.util.bean.ApplicationContextHelper;
import com.yonyou.cyx.function.exception.ServiceBizException;
import com.yonyou.cyx.function.utils.common.StringUtils;
import com.yonyou.cyxdms.framework.bean.dto.user_info.LoginInfoDto;
import com.yonyou.cyxdms.sysAuth.service.constants.ManageDictCodeConstants;
import com.yonyou.cyxdms.sysAuth.service.constants.StringUtil;
import com.yonyou.cyxdms.sysAuth.service.entity.PermissionCtrlPO;
import com.yonyou.cyxdms.sysAuth.service.entity.role.RolePositionPO;
import com.yonyou.cyxdms.sysAuth.service.mapper.PermissionCtrlMapper;
import com.yonyou.cyxdms.sysAuth.service.mapper.RoleMapper;
import com.yonyou.cyxdms.sysAuth.service.mapper.RolePositionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色受控权限实现类
 *
 * @author yll
 * @date 2016年8月8日
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RoleCtrlServiceImpl implements RoleCtrlService {

    @Autowired
    private RoleService roleService;
    @Autowired
    PermissionCtrlMapper permissionCtrlMapper;
    @Autowired
    RolePositionMapper rolePositionMapper;
    @Autowired
    private RoleMapper roleMapper;


    /**
     * 加载所有受控权限
     *
     * @author dengqiang
     * @since 2019/7/10
     */
    @Override
    public Map<String, String> queryMenuCtrl(Integer permissionType, String id) {
        List<String> idList = new ArrayList<>();
        if (id != null) {
            String[] ids = id.split(",");
            for (String splId : ids) {
                idList.add(splId);
            }
        }
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        List<Map<String, Object>> list = roleMapper.findAllPermissionCtrl(permissionType, idList, ManageDictCodeConstants.STATUS_IS_YES,loginInfoDto.getAppId());
        Map<String, String> basicresult = new HashMap<>();
        StringUtil.doBasicresult(list, basicresult);
        return basicresult;
    }

    /**
     * 修改一条受控权限
     *
     * @author dengqiang
     * @since 2019/7/12
     */
    @Override
    public void editPermissionCtrl(Integer permissionType, String permissionId, String ctrlCode, Integer type) {
        if (StringUtils.isNullOrEmpty(permissionId)) {
            throw new ServiceBizException("角色id不能为空");
        }
        if (StringUtils.isNullOrEmpty(ctrlCode)) {
            throw new ServiceBizException("控制代码不能为空");
        }
        permissionCtrlMapper.updatePermissionCtrl(ManageDictCodeConstants.STATUS_IS_YES, Integer.parseInt(permissionId), ctrlCode, permissionType);
    }


    /**
     * @author dengqiang
     * @since 2019/7/12
     */
    @Override
    public void deleteMenuByPermissionId(Integer permissionType, String permissionId, Integer type) {
        List<Map<String, Object>> list = permissionCtrlMapper.selectPermissionCtrlId(Integer.parseInt(permissionId), permissionType, type);
        for (int i = 0; i < list.size(); i++) {
            Object ctrlId = list.get(i).get("PERMISSION_CTRL_ID");
            permissionCtrlMapper.deletePermissionCtrl(Integer.parseInt(ctrlId.toString()));

        }
    }

    /**
     * 添加一条受控权限
     *
     * @author adu
     * @since 2019/9/23
     */
    @Override
    public Integer addPermissionCtrl(Integer permissionType, String permissionId, String ctrlCode,
                                     Integer type) {
        if (StringUtils.isNullOrEmpty(permissionType)) {
            throw new ServiceBizException("权限类型不能为空");
        }
        if (StringUtils.isNullOrEmpty(permissionId)) {
            throw new ServiceBizException("角色id不能为空");
        }
        if (StringUtils.isNullOrEmpty(ctrlCode)) {
            throw new ServiceBizException("控制代码不能为空");
        }
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        PermissionCtrlPO permissionctrlpo = new PermissionCtrlPO();
        permissionctrlpo.setAppId(loginInfoDto.getAppId());
        permissionctrlpo.setPermissionId(Integer.parseInt(permissionId));
        permissionctrlpo.setPermissionType(permissionType);
        permissionctrlpo.setCtrlCode(ctrlCode);
        permissionctrlpo.setType(type);
        permissionctrlpo.setIsFlexible(ManageDictCodeConstants.STATUS_IS_NOT);
        permissionctrlpo.setIsEmpowered(ManageDictCodeConstants.STATUS_IS_YES);
        permissionCtrlMapper.insert(permissionctrlpo);

        return permissionctrlpo.getPermissionCtrlId();
    }

    @Override
    public void deletePermissionCtrl(Integer permissionType, String permissionId, String ctrlCode, Integer type) {
        List<Map<String, Object>> list = permissionCtrlMapper.selectPermissionCtrlIds(permissionType, Integer.valueOf(permissionId), ctrlCode, type);
        for (int i = 0; i < list.size(); i++) {
            Object ctrlId = list.get(i).get("PERMISSION_CTRL_ID");
            permissionCtrlMapper.deletePermissionCtrl(Integer.parseInt(ctrlId.toString()));
        }
    }

    @Override
    public Integer addRolePosition(String ctrlCode, String permissionId) {
        if (StringUtils.isNullOrEmpty(permissionId)) {
            throw new ServiceBizException("角色id不能为空");
        }
        if (StringUtils.isNullOrEmpty(ctrlCode)) {
            throw new ServiceBizException("控制代码不能为空");
        }
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        RolePositionPO rolepositionpo = new RolePositionPO();
        rolepositionpo.setPositionCode(Integer.valueOf(ctrlCode));
        rolepositionpo.setRoleId(Long.valueOf(permissionId));
        rolePositionMapper.insert(rolepositionpo);
        return rolepositionpo.getRolePositionId();
    }

    @Override
    public void deleteRolePosition(String roleId) {
        //  List<Map<String, Object>> list = rolePositionMapper.selectRolePermissionId(ctrlCode, Integer.parseInt(roleId));
        //  for (int i = 0; i < list.size(); i++) {
        //      String ctrlId = list.get(i).get("ROLE_POSITION_ID").toString();
        rolePositionMapper.deleteRolePermission(Integer.valueOf(roleId));
        // }

    }


}
